package com.lewei.print;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;

import com.ibm.icu.text.DecimalFormat;
import com.lewei.utils.Constants;

/**OL35SCHINDLERCN迅达英文标签打印*/
public class OL35SCHINDLER implements Printable{
	/** 标签样式 */
	private String mainType = "OL35";
	/** 试验单位 */
	private String testingAgency = "ETC";
	/** 日期 */
	private String prodDatea = "2017-02-09";

	private String norm = "EN81";
	/** 编号 */
	private String typeNo = "900540G23R6SA15N";

	private String typeNo1 = "900540G23R6SA15N";
	/** 出厂编号 */
	private String serialNo = "200052-951";
	/** 张紧力 */
	private String fensionForce = "";
	/** 提拉力 */
	private String pullForce = "";

	/** 限速器名称 */
	private String elevatorNo = "OL35";

	/** Vns */
	private String vns = "1.60";

	/** Vcs */
	private String vcs = "1.84";

	/** Vts */
	private String vts = "2.00";

	/** 钢丝绳粗细 */
	private String lineThickness = "6.0";

	/** FbrakeMin */
	private String fbrakeMin = "500";

	/** 标签样式 */
	private String labelType = "EN81";

	/** 型号 */
	// private String modelNo="900540";
	private String modelNo = "80420";

	private Double diameter;

	/** 证书编号 */
	// private String certificateNo="TUV-A-AT-1-99-002-CEGB-6";
	private String certificateNo = "TUV-A-AT-1-98-001-CEGB-6";

	/** 左边距 */
	private double paddingLeft = 10;

	/** 上边距 */
	private double paddingTop = 20;

	/** 打印机名称 */
	private String printerName = "Foxit Reader Plus Printer";

	
	
	public String getNorm() {
		return norm;
	}

	public void setNorm(String norm) {
		this.norm = norm;
	}

	public Double getDiameter() {
		return diameter;
	}

	public void setDiameter(Double diameter) {
		this.diameter = diameter;
	}

	public String getFensionForce() {
		return fensionForce;
	}

	public void setFensionForce(String fensionForce) {
		this.fensionForce = fensionForce;
	}

	public String getPullForce() {
		return pullForce;
	}

	public void setPullForce(String pullForce) {
		this.pullForce = pullForce;
	}

	public String getMainType() {
		return mainType;
	}

	public void setMainType(String mainType) {
		this.mainType = mainType;
	}

	public String getTestingAgency() {
		return testingAgency;
	}

	public void setTestingAgency(String testingAgency) {
		this.testingAgency = testingAgency;
	}




	public String getProdDatea() {
		return prodDatea;
	}

	public void setProdDatea(String prodDatea) {
		this.prodDatea = prodDatea;
	}

	public String getTypeNo() {
		return typeNo;
	}

	public void setTypeNo(String typeNo) {
		this.typeNo = typeNo;
	}

	public String getTypeNo1() {
		return typeNo1;
	}

	public void setTypeNo1(String typeNo1) {
		this.typeNo1 = typeNo1;
	}

	public String getSerialNo() {
		return serialNo;
	}

	public void setSerialNo(String serialNo) {
		this.serialNo = serialNo;
	}

	public String getElevatorNo() {
		return elevatorNo;
	}

	public void setElevatorNo(String elevatorNo) {
		this.elevatorNo = elevatorNo;
	}

	public String getVns() {
		return vns;
	}

	public void setVns(String vns) {
		this.vns = vns;
	}

	public String getVcs() {
		return vcs;
	}

	public void setVcs(String vcs) {
		this.vcs = vcs;
	}

	public String getVts() {
		return vts;
	}

	public void setVts(String vts) {
		this.vts = vts;
	}

	public String getLineThickness() {
		return lineThickness;
	}

	public void setLineThickness(String lineThickness) {
		this.lineThickness = lineThickness;
	}

	public String getFbrakeMin() {
		return fbrakeMin;
	}

	public void setFbrakeMin(String fbrakeMin) {
		this.fbrakeMin = fbrakeMin;
	}

	public String getLabelType() {
		return labelType;
	}

	public void setLabelType(String labelType) {
		this.labelType = labelType;
	}

	public String getModelNo() {
		return modelNo;
	}

	public void setModelNo(String modelNo) {
		this.modelNo = modelNo;
	}

	public String getCertificateNo() {
		return certificateNo;
	}

	public void setCertificateNo(String certificateNo) {
		this.certificateNo = certificateNo;
	}

	public double getPaddingLeft() {
		return paddingLeft;
	}

	public void setPaddingLeft(double paddingLeft) {
		this.paddingLeft = paddingLeft;
	}

	public double getPaddingTop() {
		return paddingTop;
	}

	public void setPaddingTop(double paddingTop) {
		this.paddingTop = paddingTop;
	}

	public String getPrinterName() {
		return printerName;
	}

	public void setPrinterName(String printerName) {
		this.printerName = printerName;
	}

	/**
	 * 设置边距。
	 * 
	 * @param paddingLeft
	 * @param paddingTop
	 */
	public void setPadding(double paddingLeft, double paddingTop) {
		this.paddingLeft = paddingLeft;
		this.paddingTop = paddingTop;
	}

	public OL35SCHINDLER(String printerName) {
		super();
		this.printerName = printerName;
	}

	public OL35SCHINDLER() {
		super();
	}

	/**
	 * @param Graphic指明打印的图形环境
	 * @param PageFormat指明打印页格式
	 *            （页面大小以点为计量单位，1点为1英才的1/72，1英寸为25.4毫米。A4纸(210mm×297mm)大致为595×
	 *            842点）比例约等于2.83
	 * @param pageIndex指明页号
	 **/

	@Override
	public int print(Graphics gra, PageFormat pf, int pageIndex)
			throws PrinterException {
		// TODO Auto-generated method stub
		Component c = null;
		// 转换成Graphics2D
		Graphics2D g2 = (Graphics2D) gra;
		// 设置打印颜色为黑色
		g2.setColor(Color.black);

		// 打印起点坐标
		double x = pf.getImageableX() + 60;
		double y = pf.getImageableY();

		switch (pageIndex) {
		case 0:
			// if(lenFlag.equals("E")){
			Font font = new Font("Calibri", Font.BOLD, 8);
			g2.setFont(font);
			g2.drawString(typeNo, (float) (x + 6 ), (float) (y + 8));//900540....
			String filename = "C:\\toolsZ\\labelPrint\\codeZ\\serialNo.jpg";
			Image Img = null;
			try {
				Img = ImageIO.read(new File(filename));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			g2.drawImage(Img, (int) x - 53 , (int) y + 36, 55, 55, c);
			font = new Font("Calibri", Font.BOLD, 9);
			g2.setFont(font);
			g2.drawString(serialNo, (float) (x + 8  ), (float) (y + 31));// 中间的出厂编号200066_XXX
			
			g2.drawString(mainType, (float) (x + 27), (float) (y + 52));// OL35M
			font = new Font("Calibri", Font.BOLD, 7);
			g2.setFont(font);
			g2.drawString(prodDatea, (float) (x + 25), (float) (y + 66));// 时间
			font = new Font("Calibri", Font.BOLD,7);
			g2.setFont(font);
			if(norm.equals("EN81")){
				g2.drawString(norm, (float) (x - 40), (float) (y + 95));// En81
			}else{
				font = new Font("Calibri", Font.BOLD,6);
				g2.setFont(font);
				g2.drawString(norm, (float) (x - 43), (float) (y + 95));// En81
			}
			
			filename = "C:\\toolsZ\\labelPrint\\codeZ\\typeNo.jpg";//typeNo
			try {
				Img = ImageIO.read(new File(filename));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			g2.drawImage(Img, (int) x + 100, (int) y + 3, 80, 80, c);
			font = new Font("Calibri", Font.BOLD, 9);
			g2.setFont(font);
			g2.drawString(pullForce, (float) (x + 73), (float) (y + 80));// 提拉力800N
			g2.drawString(mainType, (float) (x-3 ), (float) (y + 79));// OL35M
			
//			font = new Font("Calibri", Font.BOLD, 7);
//			g2.setFont(font);
			
//			DecimalFormat df = new DecimalFormat("#.00");
//			df.format(Double.valueOf(vcs))
			g2.drawString(OL35SCHINDLERCN.keepTwoPoint(vns), (float) (x - 15), (float) (y + 101));
			g2.drawString(OL35SCHINDLERCN.keepTwoPoint(vcs), (float) (x + 28 ), (float) (y + 101));
			g2.drawString(OL35SCHINDLERCN.keepTwoPoint(vts), (float) (x + 70), (float) (y + 101));

			font = new Font("Calibri", Font.BOLD, 9);
			g2.setFont(font);
			String diameterStr = String.valueOf(diameter);
			g2.drawString(String.valueOf(diameterStr), (float) (x + 136),(float) (y + 97));// 左侧右下角粗细8.0mm

			font = new Font("Calibri", Font.BOLD, 7);
			g2.setFont(font);
			g2.drawString(mainType, (float) (x + 240), (float) (y + 28));// 右侧的80420
			String temp1 = "";
			if(norm.equals("EN81")){
				temp1 = "TUV-A-AT-A-98-101-CEGB-8";
			}else if(norm.equals("EN81-20")){
				temp1 = "TUV-A-AT-A-98-101-CEGB-9";
			}
			g2.drawString(temp1, (float) (x + 200), (float) (y + 38));// 右侧的证书号
			font = new Font("Calibri", Font.BOLD, 8);
			g2.setFont(font);
			g2.drawString(prodDatea, (float) (x + 230), (float) (y + 55));// 时间
			

			return PAGE_EXISTS;
		default:
			return NO_SUCH_PAGE;
		}
	}

	public static void main(String[] args) {

		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		p.setSize(350, 210);// 纸张大小
		// A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
		p.setImageableArea(0, 0, 350, 210);
		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(new OL35(), pf);

		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();

		HashAttributeSet hs = new HashAttributeSet();

		String printerName = "HP LaserJet Professional P1106";
		// String printerName = "Send To OneNote 2016";
		hs.add(new PrinterName(printerName, null));

		PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);

		try {
			job.setPrintService(pss[0]);
		} catch (PrinterException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// 设置打印类
		job.setPageable(book);

		try {
			// 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			// boolean a=job.printDialog();
			// if(a)
			// {
			job.print();
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}

	public void printcode() {

		// 通俗理解就是书、文档
		Book book = new Book();

		PageFormat pageFormat = new PageFormat();
		// 打印方向
		pageFormat.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper paper = new Paper();
		paper.setSize(400, 300);// 纸张大小
		// if(lenFlag.equals("E")){
		// // A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
		// paper.setImageableArea(Constants.LABELLEFT900540E,
		// Constants.LABELTOP900540E, 400, 300);
		// }else if(lenFlag.equals("G")&&modelNo.equals("900540")){
		// paper.setImageableArea(Constants.LABELLEFT900540-3,
		// Constants.LABELTOP900540-5, 400, 300);
		// }else{
		paper.setImageableArea(Constants.LABELLEFTOL35SCHINDLER,
				Constants.LABELTOPOL35SCHINDLER, 400, 300);
		System.out.println("打印设置标签位置: 左:" + Constants.LABELLEFTOL35SCHINDLER + ";上:" +Constants.LABELTOPOL35SCHINDLER);
		// }

		pageFormat.setPaper(paper);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(this, pageFormat);

		// 获取打印服务对象
		PrinterJob printJob = PrinterJob.getPrinterJob();

		HashAttributeSet hashAttributeSet = new HashAttributeSet();

		hashAttributeSet.add(new PrinterName(printerName, null));

		PrintService[] printServices = PrintServiceLookup.lookupPrintServices(
				null, hashAttributeSet);

		try {
			printJob.setPrintService(printServices[0]);
		} catch (PrinterException e1) {
			e1.printStackTrace();
			throw new RuntimeException(e1.getMessage());
		}

		// 设置打印类
		printJob.setPageable(book);
		try {
			// 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			// boolean a=job.printDialog();
			// if(a)
			// {
			printJob.print();
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
		}

	}
}
